package cn.edu.nuc.carsystem.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.nuc.carsystem.dto.PageInfo;
import cn.edu.nuc.carsystem.model.Bank;
import cn.edu.nuc.carsystem.model.City;
import cn.edu.nuc.carsystem.model.Company;
import cn.edu.nuc.carsystem.model.Conrtuny;
import cn.edu.nuc.carsystem.model.Custome;
import cn.edu.nuc.carsystem.model.InsureCard;
import cn.edu.nuc.carsystem.model.Loan;
import cn.edu.nuc.carsystem.service.interfaces.BankService;
import cn.edu.nuc.carsystem.service.interfaces.CityService;
import cn.edu.nuc.carsystem.service.interfaces.CompanyService;
import cn.edu.nuc.carsystem.service.interfaces.InsureCardService;
import cn.edu.nuc.carsystem.service.interfaces.LoanService;
/**
 * 保险单controller
 * @author 杨晓庆
 *
 */
@RequestMapping("/custome/insureCard")
@Controller
public class InsureCardController {
   @Autowired
   private InsureCardService insureCardService;
   @Autowired
   private CompanyService companyService;
   @Autowired
   private LoanService loanService;
   @InitBinder  
   public void initBinder(WebDataBinder binder) {  
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
       dateFormat.setLenient(false);  
       binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
   }
   /**
    * 添加保险单
    * @param insureCard
    * @return
    */
   @RequestMapping(value="/insert",method=RequestMethod.POST)
	public String insert(InsureCard insureCard,HttpSession session){
	 Custome custome=(Custome) session.getAttribute("custome");
	 insureCard.setCustomeid(custome.getCustomeid());
    Integer count=insureCardService.insert(insureCard);
    if(count>0){
	   return "redirect:/custome/loan/addLoan";
    }
    else
       return "/custome/insureCard/addinsurecard";
   }
   /**
    * 跳转添加保险单页面
    *
    * @return
    */
   @RequestMapping(value="/addinsureCard",method=RequestMethod.GET)
	public ModelAndView addBank(){
	   ModelAndView model=new ModelAndView("custome/addinsurecard");
       List<Company> list=companyService.select();
       model.addObject("list",list);
       System.out.println("公司"+list);
       return model;
   }
   /**
    * 查询保险单
    */
   @RequestMapping(value="/findAll",method=RequestMethod.GET)
   public ModelAndView findAll(HttpSession session){
	   ModelAndView model=new ModelAndView("custome/insurecard");
	   Custome custome=(Custome) session.getAttribute("custome");
	   
	   List<InsureCard> list=insureCardService.selectByCosumeid(custome.getCustomeid());
	   System.out.println("保险单"+list);
	   model.addObject("list",list);
	   return model;
   }
   @RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(Integer insureid){
	   ModelAndView model=new ModelAndView("custome/updateinsurecard");
	  
	   InsureCard insure=insureCardService.selectByPrimaryKey(insureid);
	   System.out.println("保险单"+insure);
	   model.addObject("insure",insure);
	   return model;
   }
   @RequestMapping(value="/updateCard",method=RequestMethod.POST)
	public ModelAndView update(InsureCard insureCard,Loan loan,HttpSession session){
	   ModelAndView model;
	   Custome custome=(Custome) session.getAttribute("custome");
	   insureCard.setCustomeid(custome.getCustomeid());
	   loan.setCustomeid(custome.getCustomeid());
	   
	   Integer count=insureCardService.updateByPrimaryKeySelective(insureCard);
	   if(count>0){
		      
		 Integer count1=loanService.updateByPrimaryKeySelective2(loan);
		 model =new ModelAndView("redirect:/custome/insureCard/findAll");
	   }else{
		   model=new ModelAndView("custome/updateinsurecard");
			  
		   InsureCard insure=insureCardService.selectByPrimaryKey(insureCard.getInsureid());
		   model.addObject("insure",insure);
	   }
	  
	  
	   return model;
   }


}
